package com.seele.tigerwallet.jpush;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.seele.tigerwallet.ui.MainActivity;
import com.seele.tigerwallet.ui.activtiy.WelcomeActivity;
import com.seele.tigerwallet.ui.application.SeeleApplication;
import com.seele.tigerwallet.utils.Utils;

import cn.jpush.android.api.JPushInterface;

public class JPushStateReceiver extends BroadcastReceiver{
    private static final String TAG = JPushStateReceiver.class.getSimpleName();
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "SeeleReceiver action:" + intent.getAction());
        if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
            try {
                Log.i(TAG, "SeeleReceiver ACTION_NOTIFICATION_OPENED:" + intent.getAction());
                boolean isExist = Utils.isExistMainActivity(MainActivity.class);
                Log.i(TAG, "MainActivity is exist:" + isExist);
                Intent startIntent = new Intent(context, MainActivity.class);
                if (!isExist) {
                    Activity topActivity = SeeleApplication.getInstance().getTopActivity();
                    if (topActivity != null) {
                        Log.i(TAG, "topActivity is:" + topActivity.getClass().getSimpleName());
                        startIntent = new Intent(context, topActivity.getClass());
                    } else {
                        startIntent =  context.getPackageManager().getLaunchIntentForPackage("com.seele.tigerwallet");
                    }
                }
                context.startActivity(startIntent);
            }catch (Exception e) {
                Log.e(TAG, "JPushStateReceiver start Activity failed:" + e.getMessage());
            }
        }
    }
}
